﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Task.Data;
using CentralOffice.Task.BusinessActions;

using ApplicationTable = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsDataTable;
using ApplicationRow = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsRow;

using ApprovalTable = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsDataTable;
using ApprovalRow = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsRow;
using System.Data;

namespace CentralOffice.WebClient.Tasks
{
    public partial class ApplicationExecuteInfo : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string applicationId = HttpContext.Current.Request.QueryString["id"];
                ApplicationRow application = CentralOffice.Task.BusinessActions.Applications.GetById(new Guid(applicationId));

                lblApplicationCode.Text = application.IsApplicationCodeNull() ? "" : application.ApplicationCode;
                lblApplicant.Text = application.Applicant;
                lblApprovalContent.Text = application.ApprovalContent;
                lblDateOfApplication.Text = application.DateOfApplication.ToString();
                lblName.Text = application.Name;
                lblStatus.Text = EnumUtil.GetDisplayName(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, application.Status);

                if (!application.IsAttachmentsNull() && application.Attachments.Length > 0)
                {
                    lblAttachments.Visible = true;
                    SiteUtil.GenernateDownloadHyperLink(lblAttachments, application.Attachments);
                }

                txtExecutiveDescription.Text = application.IsExecutiveDescriptionNull() ? "" : application.ExecutiveDescription;
            }
        }

        protected void gvApproval_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApprovalRow approval = ((DataRowView)e.Row.DataItem).Row as ApprovalRow;

                if (approval[(approval.Table as ApprovalTable).DateApprovedColumn.ColumnName] != DBNull.Value)
                {
                    if (approval.ResultApproved)
                    {
                        e.Row.Cells[6].Text = "通过";
                    }
                    else
                    {
                        e.Row.Cells[6].Text = "<span class=\"warning\">否决</span>";
                    }
                }

                if (!approval.IsApprovalAttachmentsNull())
                {
                    HyperLink link = e.Row.Cells[7].Controls[0] as HyperLink;
                    SiteUtil.GenernateDownloadHyperLink(link, approval.ApprovalAttachments);
                }
                //else
                //{
                //    link = e.Row.Cells[7].Controls[0] as HyperLink;
                //    SiteUtil.GenernateDownloadHyperLink(link, approval.ApprovalAttachments);

                //}

                if (approval[(approval.Table as ApprovalTable).PreviousDateApprovedColumn.ColumnName] != DBNull.Value)
                {
                    if (approval.PreviousResultApproved)
                    {
                        e.Row.Cells[11].Text = "通过";
                    }
                    else
                    {
                        e.Row.Cells[11].Text = "<span class=\"warning\">否决</span>";
                    }
                }

                if (approval.ApprovedBy == CommonUtil.TrimAll(ApplicationContext.ActualUserName) && approval[(approval.Table as ApprovalTable).DateApprovedColumn.ColumnName] == DBNull.Value)
                {
                    e.Row.Cells[12].Controls[0].Visible = true;
                }
                else
                {
                    e.Row.Cells[12].Controls[0].Visible = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string applicationId = HttpContext.Current.Request.QueryString["id"];
            ApplicationRow application = CentralOffice.Task.BusinessActions.Applications.GetById(new Guid(applicationId));

            ApplicationDataSet applicationDataSet = application.Table.DataSet as ApplicationDataSet;

            application.Status = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "已执行");
            application.ExecutiveDescription = txtExecutiveDescription.Text;

            application.LastModifiedBy = ApplicationContext.ActualUserId;
            application.ModifiedTime = DateTime.Now;

            CentralOffice.Task.BusinessActions.Applications.Update(applicationDataSet);
            Response.Redirect("ApplicationExecute.aspx");
        }

    }
}
